package com.lijy.likebutton.view

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationSet
import android.view.animation.OvershootInterpolator
import com.lijy.likebutton.R
import com.lijy.likebutton.util.DimenUtils

/**
 *@time:     2020/12/1 0001
 *@author:   Lijy

 *@description:
 */
class ThumbsView @JvmOverloads constructor(
    var mIsSelectedFlag: Boolean = false, context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr), View.OnClickListener {

    private val startColor = Color.parseColor("#88e24d3d")
    private val iconWidth = DimenUtils.dp2px(context, 20f)
    private val iconHeight = DimenUtils.dp2px(context, 20f)
    private val iconPadding = DimenUtils.dp2px(context, 18f)

    //如果圆半径的最大值等于view的最大值得话，会有被覆盖的部分。所以-3
    private val circleMaxRadius = (iconWidth + iconPadding) / 2 - 3
    private val circleMinRadius = circleMaxRadius / 3 * 2

    private var selectedIcon =
        BitmapFactory.decodeResource(resources, R.drawable.ic_messages_like_selected)
    private var selectedShiningIcon =
        BitmapFactory.decodeResource(resources, R.drawable.ic_messages_like_selected_shining)
    private var unSelectedIcon =
        BitmapFactory.decodeResource(resources, R.drawable.ic_messages_like_unselected)


    //圆的透明度
    private var circleAlpha = 255 / (circleMaxRadius-circleMinRadius)

    /*---------------画笔start--------------*/
    private val iconPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val circlePath = Path()
    /*---------------画笔end--------------*/

    /*--------------动画start---------------*/

    private var unSelectIconScale = 0f
        set(value) {
            field = value
            val matrix = Matrix()
            matrix.setScale(value, value)
            unSelectedIcon =
                BitmapFactory.decodeResource(resources, R.drawable.ic_messages_like_unselected)
            unSelectedIcon = Bitmap.createBitmap(
                unSelectedIcon,
                0,
                0,
                unSelectedIcon.width,
                unSelectedIcon.height,
                matrix,
                true
            )
            postInvalidate()
        }

    private fun unSelectIconScaleAnim() {
        val iconAnim = ObjectAnimator.ofFloat(this, "unSelectIconScale", 0.5f, 1f)
        iconAnim.duration = 150
        iconAnim.interpolator = OvershootInterpolator()
        iconAnim.start()
    }

    private var selectIconScale = 0f
        set(value) {
            field = value
            val matrix = Matrix()
            matrix.setScale(value, value)
            selectedIcon =
                BitmapFactory.decodeResource(resources, R.drawable.ic_messages_like_selected)
            selectedIcon = Bitmap.createBitmap(
                selectedIcon,
                0,
                0,
                selectedIcon.width,
                selectedIcon.height,
                matrix,
                true
            )
            postInvalidate()
        }

    private var circleRadius = 0f
        set(value) {
            field = value
            Log.e("TAG", ": $value")
            circlePath.addCircle(x, y, value, Path.Direction.CW)
            circlePaint.alpha = ((circleMaxRadius - circleRadius) * circleAlpha).toInt()
            postInvalidate()
        }

    private fun selectIconScaleAnim() {
        //Icon动画
        val iconAnim = ObjectAnimator.ofFloat(this, "selectIconScale", 0.8f, 1f)
        iconAnim.duration = 150
        iconAnim.interpolator = OvershootInterpolator()
        //扩散圆半径动画
        //todo 还是有问题，感觉有点生硬
        val radiusAnim =
            ObjectAnimator.ofFloat(
                this,
                "circleRadius",
                circleMinRadius,
                circleMaxRadius
            )
        radiusAnim.duration = 150

        val animatorSet = AnimatorSet()
        animatorSet.play(iconAnim).with(radiusAnim)
        animatorSet.start()
    }

    /*--------------动画end---------------*/
    init {
        circlePaint.strokeWidth = 5f
        circlePaint.color = startColor
        circlePaint.style = Paint.Style.STROKE

        circlePath.addCircle(x, y, circleMaxRadius, Path.Direction.CW)
//        setOnClickListener(this)
    }

    override fun onClick(v: View?) {
//        mIsSelectedFlag = !mIsSelectedFlag
        if (mIsSelectedFlag) {
            selectIconScaleAnim()
        } else {
            unSelectIconScaleAnim()
        }
        postInvalidate()
    }

    fun startAnim (){
        if (mIsSelectedFlag) {
            selectIconScaleAnim()
        } else {
            unSelectIconScaleAnim()
        }
        postInvalidate()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        val width = (iconWidth + iconPadding).toInt()
        val height = (iconHeight + iconPadding).toInt()

        if (layoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT
            && layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT
        ) {
            setMeasuredDimension(width, height)
        } else if (layoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
            setMeasuredDimension(width, heightSize)
        } else if (layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            setMeasuredDimension(widthSize, height)
        }
    }

    override fun onDraw(canvas: Canvas) {
        val x = width / 2f
        val y = height / 2f

        if (mIsSelectedFlag) {
            /**
             * save()
             * clipPath()
             * 会让光少一个点  具体原因待查
             */
//            canvas.save()
//            canvas.clipPath(circlePath)
            //光
            canvas.drawBitmap(
                selectedShiningIcon,
                x - selectedIcon.width / 2,
                y - selectedIcon.height / 2 - 30,
                iconPaint
            )
//            canvas.restore()
            //扩散圆
            canvas.drawCircle(x, y, circleRadius, circlePaint)
            //点赞
            canvas.drawBitmap(
                selectedIcon,
                x - selectedIcon.width / 2,
                y - selectedIcon.height / 2,
                iconPaint
            )
        } else {
            //未点赞
            canvas.drawBitmap(
                unSelectedIcon,
                x - unSelectedIcon.width / 2,
                y - unSelectedIcon.height / 2,
                iconPaint
            )
        }
    }
}